package com.example.understandvideo.service;

import com.example.understandvideo.mapper.SettingDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;

@Service
@Slf4j
public class CommonService {
    @Resource
    private SettingDao settingDao;

    public String getWorkSpace() {
        String os = System.getProperty("os.name");
        if (os != null && !os.toLowerCase().startsWith("windows")) {
            return settingDao.selectByPrimaryKey(2L);
        } else {
            return settingDao.selectByPrimaryKey(1L);
        }
    }

    public String getFfmpegExcFileName() {
        String os = System.getProperty("os.name");
        if (os != null && !os.toLowerCase().startsWith("windows")) {
            return settingDao.selectByPrimaryKey(4L);
        } else {
            return settingDao.selectByPrimaryKey(3L);
        }
    }

    public String getAIModelName() {
        return settingDao.selectByPrimaryKey(5L);
    }



    public String getServerAddress() {
        return settingDao.selectByPrimaryKey(6L);
    }

    public String getSportAIQuestion() {
        return settingDao.selectByPrimaryKey(7L);
    }
    public String getSpeechAIQuestion() {
        return settingDao.selectByPrimaryKey(8L);
    }
    public String getCommonAIQuestion() {
        return settingDao.selectByPrimaryKey(9L);
    }

    public String getLocalAddress() {
        return settingDao.selectByPrimaryKey(10L);
    }

    public String getFileNameWithoutExtension(String fileName) {
        if (fileName == null || fileName.isEmpty()) {
            return null;
        }

        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex == -1) {
            return fileName; // 没有后缀，直接返回原文件名
        }

        return fileName.substring(0, dotIndex); // 截取不包含后缀的部分
    }

    public Long extractAndConvert(String filepath) {
        File file = new File(filepath);
        // 提取数字部分
        String numberStr = file.getName().replaceAll("\\D+", "");
        // 转换为 Integer
        try {
            Long number = Long.parseLong(numberStr);
            return number;
        } catch (NumberFormatException e) {
            System.err.println("无法转换为整数: " + numberStr);
            return null;
        }
    }
}
